package com.artqiyi.dollmachine.utils;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;


/**
 * 点击工具，防抖处理
 */

public class ViewClickUtil {

    public static long lastTime = 0;



    /**
     * 默认1秒内不可连续点击
     */
    public static boolean canClick() {
        return canClick(1000);
    }

    /**
     * 防连续点击处理
     *
     * @param ms 限定毫秒数
     * @return
     */
    public static boolean canClick(long ms) {
        if (System.currentTimeMillis() - lastTime > ms) {
            lastTime = System.currentTimeMillis();
            return true;
        } else {
            return false;
        }
    }


//    /**
//     * 连续点击取最后一次，延迟ms执行
//     *
//     * @param view
//     * @param ms      延迟ms执行
//     * @param action1
//     */
//    public static void clickLast(View view, long ms, Action1<Long> action1) {
//        Observable.create((Observable.OnSubscribe<Long>) subscriber -> view.setOnClickListener(v -> {
//            lastTime = System.currentTimeMillis();
//            subscriber.onNext(lastTime);
//        })).delay(ms, TimeUnit.MILLISECONDS)
//                .filter(aLong -> lastTime == aLong)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(action1);
//    }


    public static void checkEnable(View v, EditText editText) {

        v.setEnabled(false);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                v.setEnabled(!TextUtils.isEmpty(s.toString()));
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                v.setEnabled(!TextUtils.isEmpty(s.toString()));
            }
        });


    }

    /**
     * 保留两位小数<br/>
     * 请设置android:inputType="numberDecimal"
     */
    public static void keepPoint2(EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String s1 = s.toString();
                if (TextUtils.equals(s1, ".")) {
                    editText.setText("0.");
                } else if (TextUtils.equals(s1, "00")) {
                    editText.setText("0");
                }
                if (s1.contains(".") && s1.substring(s1.indexOf(".") + 1).length() > 2) {
                    editText.setText(s1.substring(0, s1.indexOf(".") + 3));
                }
                //                String s2 = editText.getText().toString();
                //                if (!TextUtils.isEmpty(s2)) {
                //                    double v = Double.parseDouble(s2);
                //                    if (v > 2000) {
                //                        editText.setText("2000.00");
                //                    }
                //                }

                editText.setSelection(editText.getText().length());

            }
        });
    }

}
